﻿<?php ob_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, target-densitydpi=medium-dpi" />

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Sign UP</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
  	<style type = "text/css">
			body{
				
				font-family: Futura;
				background-image:url("back.jpg");
				background-repeat: repeat;
				background-position: center;
			}
			p{
				color: maroon;
			}
			h1{
				font-family: Zapfino;
				color: gray;
				font-size : 40;
			
			}
		</style>
</head>
<body>
	<h1>Sign Up</h1>
	<?php
	
		$output_form = false;	//html 폼을 출력할지 말아야할지를 결정하는 변수
		$email = NULL;	//email 초기화 
		$password =NULL; // password 초기화
		$repassword =NULL; // repassword 초기화
		$first_name =NULL; //first name 초기화
		$last_name =NULL;	//last name 초기화
		$location = NULL;	//location 초기화
		$job =NULL;	// job 초기화
		$nation = NULL; //nation 초기화


		if( isset( $_POST['submit'] ) ){
			
			//이전 form입력이 있었을 경우 해당하는 text박스의 값을 해당 변수에 넣어준다.
			$email = $_POST['email'];
			$password = $_POST['password'];
			$repassword = $_POST['repassword'];
			$first_name = $_POST['firstname'];
			$last_name = $_POST['lastname'];
			$location = $_POST['location'];
			$job = $_POST['job'];
			$nation = $_POST['nation'];

			//print error message;

			if( empty( $email ) ){
				echo 'You forgot the E-mail subject.<br />';
				$output_form = true; 
			}else{
				$dbc = mysqli_connect( 'localhost' ,'root' , '', 'teresa' ) or die( 'Error connecting to MySQL server.');
						
				$query = "SELECT email FROM teresa_user WHERE email = '$email'";
                $data = mysqli_query( $dbc , $query);

				if( mysqli_num_rows( $data ) == 1 ){
					echo 'Your E-mail already existed.<br />';
					$output_form = true; 
				}
				mysqli_close($dbc);	
			}
			if( empty( $password ) ){
				echo 'You forgot the password.<br />';
				$output_form = true;
			}else if( $password != $repassword ){
				echo 'Password is not correct.<br />';
				$output_form = true;				
			}

			if( empty( $first_name ) ){
				echo 'You forgot the firstname.<br />';
				$output_form = true;
			}
			if( empty( $last_name ) ){
				echo 'You forgot the lastname.<br />';
				$output_form = true;
			}
			if( empty( $nation ) ){
				echo 'You forgot the nation.<br />';
				$output_form = true;			
			}
			if( empty( $job ) ){
				echo 'You forgot the job.<br />';
				$output_form = true;			
			}
			if( empty( $location ) ){
				echo 'You forgot the location.<br />';
				$output_form = true;			
			}


		}else{
			$output_form = true;
		}

		if( $output_form ){
			//사용자 입력 불충분시 다시 재작되는 html
			?>


			<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
  			<fieldset>
  			<table>
				<tr>
					<td>
						<p>First name:</p>
					</td>
					<td>
						<input type="text" id="firstname" name="firstname" value="<?php echo $first_name; ?>" /><br />
 					</td>
 				</tr>		
				<tr>	
					<td>
						<p>Last name:</p>
					</td>
					<td>
						<input type="text" id="lastname" name="lastname" value="<?php echo $last_name; ?>" /><br />
 					</td>
 				</tr>
				<tr>	
					<td>
						<p>E-Mail:</p>
					</td>
					<td>
						<input type="text" id="email" name="email" value="<?php echo $email; ?>" /><br />
 					</td>
 				</tr>
				<tr>	
					<td>
						<p>Password:</p>
					</td>
					<td>
						<input type="password" id="password" name="password" /><br />
					</td>
				</tr>
				<tr>
					<td>
						<p>Re-Password:</p>
					</td>	
					<td>	
						<input type="password" id="password(retype)" name="repassword" /><br />
 					</td>
 				</tr>
				<tr>	
					<td>
						<p>Location:</p>
					</td>	
					<td>	
						<input type="text" id="location" name="location" value="<?php echo $location; ?>" /><br />
 					</td>
 				</tr>
 				<tr>	
					<td>
						<p>Nation:</p>
					</td>	
					<td>	
						<select name="nation">
							<option value="Korea">    Korea    </option>
							<option value="US">    US    </option>
							<option value="Japan">    Japan    </option>
						</select>
 					</td>
 				</tr>
				<tr>	
					<td>	
						<p>Job :</p>
					</td>	
					<td>	
						<select name="job">
							<option value="Doctor">    Doctor    </option>
							<option value="Student">    Student    </option>
							<option value="Teacher">    Teacher    </option>
							<option value="Salesman">    Salesman    </option>
							<option value="Etc">    Etc    </option>
						</select>
 					</td>
 				</tr>
 				<tr>
					<td></td>
					<td>
						<input type="submit" value="submit" name="submit" />
					</td>
				</tr>
			</table>
			</fieldset>
			</form> 
			
			
			  
			<?php
		}else{
			$dbc = mysqli_connect( 'localhost' ,'root' , '1234', 'teresa' ) or die( 'Error connecting to MySQL server.');
						
			$query = "INSERT INTO teresa_user( email, password, first_name, last_name , job , nation, location)".
				"VALUES('$email', SHA( '$password' ), '$first_name' ,'$last_name' , '$job' , '$nation' , '$location' )"; 
			mysqli_query( $dbc, $query ) or die('ERROR queerying database.');
			mysqli_close($dbc);
			
			//쿠키 셋팅 및 로그인
			setcookie('user_email', $email );
            setcookie('username', $first_name);
			setcookie('user_id' , $user_id );
			$home_url = 'http://' . $_SERVER['HTTP_HOST'] . '/main.php';
			header('Location: ' . $home_url );
		}
	?>
</body>
</html>
